package edu.ncsu.csc.openid;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class OpenIDReturnHandler
 */
public class OpenIDReturnHandler extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public OpenIDReturnHandler() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		String LName = request.getParameter("openid.ext1.value.lastname");
		String FName = request.getParameter("openid.ext1.value.firstname");
		String EMail = request.getParameter("openid.ext1.value.email");
		String UId = EMail.split("@")[0];
		if(isNameCorrect(FName, LName))
			response.sendRedirect("/iTrust/j_security_check?j_username=1&j_password=pw");
		//PrintExtractedInformation(response, LName, FName, EMail, UId);
		
	
	}
	private boolean isNameCorrect(String first, String last)
	{
		return true;
	}

	private void PrintExtractedInformation(HttpServletResponse response, String LName, String FName,
			String EMail, String UId) throws IOException {
		PrintWriter pw = response.getWriter();
		pw.print("<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /><title>Test JOpenID</title></head><body><h1>You have successfully signed on!</h1>");
		pw.print("<p>" + "Last Name: " +LName + "</p>");
		pw.print("<p>" + "First Name: " + FName + "</p>");
		pw.print("<p>" + "EMail: " +EMail + "</p>");
		pw.print("<p>" + "Unity ID: " + UId + "</p>");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
